package com.design.fitment.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.design.fitment.R;
import com.design.fitment.adapter.MoreListAdapter;
import com.design.fitment.entity.MoreList;
import com.design.fitment.util.DesignNetworkUtil;
import com.design.fitment.util.RequestDataCallback;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_more_anli)
public class MoreAnliActivity extends AppCompatActivity {
    @ViewInject(R.id.radiogrop_gallery)
    private RadioGroup rgMore;
    @ViewInject(R.id.plv_more)
    private PullToRefreshListView plv;
    @ViewInject(R.id.ll_more1)
    private LinearLayout llMore1;
    @ViewInject(R.id.ll_more2)
    private LinearLayout llMore2;
    @ViewInject(R.id.radioButton_home)
    private RadioButton rbHome;
    @ViewInject(R.id.radioButton_work)
    private RadioButton rbWork;
    private MoreListAdapter mAdapter ;
    private List<MoreList.DataEntity> mList = new ArrayList<>();
    private String action = "home";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_more_anli);
        ViewUtils.inject(this);
        initMainPage();
        load(action);
    }



    private void initMainPage() {
        mAdapter = new MoreListAdapter(this,mList);
        plv.setAdapter(mAdapter);
        plv.getRefreshableView().setDivider(null);

        // 同时支持上下拉刷新
        plv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        // 设置事件

        // 设置PullToRefreshListView的监听
        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
                                     @Override
                                     public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                                         //mList.clear();
                                         //*if (mList != null)
                                         load(action);

                                     }


                                 }

        );
        rgMore.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                mList.clear();
                switch (i) {
                    case R.id.radioButton_home:
                        action = "home";
                        rbHome.setChecked(true);
                        rbWork.setChecked(false);
                        llMore1.setVisibility(View.VISIBLE);
                        llMore2.setVisibility(View.GONE);
                        load(action);
                        break;
                    case R.id.radioButton_work:
                        rbHome.setChecked(false);
                        rbWork.setChecked(true);
                        llMore1.setVisibility(View.GONE);
                        llMore2.setVisibility(View.VISIBLE);
                        action = "public";
                        load(action);
                        break;
                }
            }
        });

    }

    private void load(String action) {
        DesignNetworkUtil.getInstance().getMoreList(new RequestDataCallback() {

            @Override
            public void onSuccess(String response) {
                Gson gson = new Gson();
                MoreList moreList = gson.fromJson(response, MoreList.class);
                if (moreList != null) {
                    mList.addAll(moreList.getData());

                }
                mAdapter.notifyDataSetChanged();
                plv.onRefreshComplete();
            }

            @Override
            public void onFailure(HttpException error) {
                plv.onRefreshComplete();
            }


        }, action);
    }
    @OnClick(R.id.iv_back)
    private void OnclickBack(View view){
        this.finish();
        this.overridePendingTransition(R.anim.in_from_left, R.anim.out_from_right);
    }



}
